Page({
  data: {
    buttonList: [ // 按钮组
      { text: '提交' },
      { text: '委托记录' }
    ],
    showblock: true,

    array: [],
    arraychilren: [],
    showmodels: [''],
    showchildren: [],
    goodsId: '',
    childrendata: [],
    goodsId3: "",
    goodsSpecId3: "",
    goodsName3: "",
    options: [],
    message: 0,
    inTradeNo: '',    //订单号
    switch1: false,
    goodsQuantity: '',
    minusStatus: 'disabled',
    clickaddress: "点击此处添加收货地址",
    addshops: "../../../assets/images/icon_jia.png",
    removeshops: "../../../assets/images/icon_jian.png",
    locationimg: "../../../assets/images/icon_dingwei.png",
    index: 0,
    indexdata: 0,
    printTypeList: [], // 打印类型列表
    goodsSpecId: '',
    student: '',
    goodsName: '',
    goodsName1: '',
    goodsId1: '',
    schoolCode: '',
    info: '',
    note: '',
    userId: '',
    orderGoodsList: [],
    childrenList: [],
    form: {
      userId: '',
      printType: '',
      number: '',
      student: '',
      file: ''
    },
    province: '',
    city: '',
    county: '',
    detil: '',
    userName: '',
    mobile: '',
    isObey: false,
    id: "",
    switchText: {
      yes: '是',
      no: '否'
    },
    errcode: ''    //判断errcode为0就是成功，否则就没有数据
  },
  onLoad (option) {
    wx.getStorage({      //小程序获取Storage存储的数据
      key: 'userInfo',
      success: (res) => {
        this.setData({
          schoolCode: res.data.schoolCode,
          userId: res.data.userId
        })
      }
    })
    if (option.city) {
      this.setData({
        province: option.province,
        city: option.city,
        county: option.county,
        detil: option.detil,
        userName: option.userName,
        mobile: option.mobile
      })
    } else {
      wx.getStorage({      //小程序获取Storage存储的数据
        key: 'userInfo',
        success: (res) => {
          this.setData({
            schoolCode: res.data.schoolCode,
            userId: res.data.userId
          })
          console.log(res.data.name, "拿到getStorage")
          this.getaddressList();
        }
      })
    }
    if (this.data.isObey != true) {
      wx.getStorage({      //小程序获取Storage存储的数据
        key: 'userInfo',
        success: (res) => {
          this.setData({
            schoolCode: res.data.schoolCode,
            userId: res.data.userId
          })
          this.getselect();
        }
      })
    } else {
      this.getschooltype();
    }
  },
  getselectid () {
    const request = {
      schoolCode: this.data.schoolCode,
      goodsId: this.data.goodsId
    }
    wx.$API['workService/specget'](request).then((response) => {
      this.setData({
        goodsSpecId1: response.data[0].goodsSpecId,
      })
      const childs = response.data.map(v => v.goodsSpecName);
      this.setData({
        showchildren: childs, arraychilren: response.data
      })
    }).catch((error) => {
      wx.showToast({
        title: error,
        icon: "none"
      })
      console.log('getselectid error', error)
    })
  },
  getselectidto () {
    const request = {
      schoolCode: this.data.schoolCode,
      goodsId: this.data.goodsId1
    }
    wx.$API['workService/specget'](request).then((response) => {
      this.setData({
        goodsSpecId2: response.data[0].goodsSpecId
      })
    }).catch((error) => {
      wx.showToast({
        title: error,
        icon: "none"
      })
      console.log('getselectidto error', error)
    })
  },
  bindPickerChangelet (e) {
    this.setData({
      indexdata: e.detail.value
    })
  },
  bindPickerChange: function (e) {
    if (this.data.isObey == true) {
      this.setData({
        index: e.detail.value,
      })
      if (this.data.index == 0) {
        wx.showToast({
          title: '请选择文件',
          icon: 'none',
          duration: 2000
        })
        this.setData({
          showblock: true
        })
      } else {
        this.setData({
          showblock: false,
          goodsId: this.data.array[this.data.index].goodsId,
          goodsName: this.data.array[this.data.index].goodsName
        })
        this.getselectid();
      }
    } else {
      this.setData({
        index: e.detail.value
      })
      if (this.data.index == 0) {
        wx.showToast({
          title: '请选择文件',
          icon: 'success',
          duration: 2000
        })
        this.setData({
          showblock: true
        })
      } else {
        this.setData({
          goodsId3: this.data.array[this.data.index].goodsId,
          goodsName3: this.data.array[this.data.index].goodsName,
          showblock: false
        })
        this.getselect3id();
      }
    }
  },
  getselect3id () {
    const request = {
      schoolCode: this.data.schoolCode,
      goodsId: this.data.goodsId3
    }
    wx.$API['workService/specget'](request).then((response) => {
      this.setData({
        goodsSpecId3: response.data[0].goodsSpecId,
      })
      const childs = response.data.map(v => v.goodsSpecName);
      this.setData({
        showchildren: childs, arraychilren: response.data
      })
    }).catch((error) => {
      wx.showToast({
        title: error,
        icon: "none"
      })
      console.log('getselect3id error', error)
    })
  },
  getselect () {    //文件名称select下拉框接口数据
    const request = {
      schoolCode: this.data.schoolCode,
      goodsType: 0
    }
    wx.$API['workService/goodslist'](request).then((response) => {
      this.setData({
        goodsId3: response.data[0].goodsId,
        goodsName3: response.data[0].goodsName
      })
      const arr = response.data.map(v => v.goodsName);
      this.setData({
        showmodels: ['', ...arr], array: [{}, ...response.data]
      })
    }).catch((error) => {
      wx.showToast({
        title: error,
        icon: "none"
      })
      console.log('getselect error', error)
    })
  },
  getschooltype () {
    const request = {
      schoolCode: this.data.schoolCode,
      goodsType: 0
    }
    wx.$API['workService/goodslist'](request).then((response) => {
      this.setData({
        goodsId: response.data[0].goodsId,
        goodsName: response.data[0].goodsName
      })
      this.setData({
        goodsId: response.data[0].goodsId,
        goodsName: response.data[0].goodsName
      })
      this.getsetitem1();
      const arr = response.data.map(v => v.goodsName);
      this.setData({
        showmodels: ['', ...arr], array: [{}, ...response.data],
        childrendata: response.data
      })
    }).catch((error) => {
      wx.showToast({
        title: error,
        icon: "none"
      })
      console.log('getschooltype error', error)
    })
  },
  getschooltypeto () {
    const request = {
      schoolCode: this.data.schoolCode,
      goodsType: 1
    }
    wx.$API['workService/goodslist'](request).then((response) => {
      this.setData({
        goodsName1: response.data[0].goodsName,
        goodsId1: response.data[0].goodsId
      })
      this.getselectidto();
    }).catch((error) => {
      wx.showToast({
        title: error,
        icon: "none"
      })
      console.log('getschooltypeto error', error)
    })
  },
  getsetitem1 () {
    this.getselectid();
  },
  getsetitem () {
    this.getselect3id();
  },
  getdetailList () {
    this.getsetitem();
    const request = {
      schoolCode: this.data.schoolCode,
      userId: this.data.userId,
      toName: this.data.userName,
      toPhone: this.data.mobile,
      toProv: this.data.province,
      toCity: this.data.city + "," + this.data.county,
      toAddress: this.data.detil,
      note: this.data.note,
      orderGoodsList: [
        {
          goodsId: this.data.goodsId3,
          goodsQuantity: this.data.goodsQuantity,
          goodsSpecId: this.data.goodsSpecId3,
          goodsName: this.data.goodsName3
        }
      ],
      deliveryCharge: 1
    }
    wx.$API['workService/orderadd'](request).then((response) => {
      this.setData({
        inTradeNo: response.data[0].inTradeNo
      })
      wx.navigateTo({
        url: '../../workService/paymentconfirm/index?inTradeNo=' + this.data.inTradeNo + '&goodsName=' + this.data.goodsName3
      })
    }).catch((error) => {
      wx.showToast({
        title: error,
        icon: "none"
      })
      console.log('getdetailList error', error)
    })
  },
  /**
   * @see 获取地址信息
   */
  getaddressList () {
    const request = {

    }
    wx.$API['workService/getaddress'](request).then((response) => {
      const { paramsdata } = this.data;
      this.setData({
        province: response.data[0].province,
        city: response.data[0].city,
        county: response.data[0].county,
        detil: response.data[0].detil,
        userName: response.data[0].userName,
        mobile: response.data[0].mobile,
        paramsdata: response.data,
        errcode: 0
      })
    }).catch((error) => {
      this.setData({
        errcode: 1
      })
    })
  },

  /* 输入框事件 */
  bindManual: function (e) {
    var goodsQuantity = e.detail.value;
    // 将数值与状态写回
    this.setData({
      goodsQuantity: goodsQuantity
    });
  },
  toggleObey () {
    this.setData({
      isObey: !this.data.isObey
    })
    if (this.data.isObey == true) {
      this.getschooltype();   //成绩单
      this.getschooltypeto();  //信封包装
    }
    else {
      this.getselect();
    }
  },
  onChange (event) {
    const detail = event.detail;
    this.setData({
      'switch1': detail.value
    })
  },
  getSwitch () {
    const request = {
      schoolCode: this.data.schoolCode,
      userId: this.data.userId,
      toName: this.data.userName,
      toPhone: this.data.mobile,
      toProv: this.data.province,
      toCity: this.data.city + "," + this.data.county,
      toAddress: this.data.detil,
      note: this.data.note,
      orderGoodsList: [
        {
          goodsId: this.data.goodsId,
          goodsQuantity: this.data.goodsQuantity,
          goodsSpecId: this.data.goodsSpecId1,
          goodsName: this.data.goodsName
        },
        {
          goodsId: this.data.goodsId1,
          goodsQuantity: this.data.goodsQuantity,
          goodsSpecId: this.data.goodsSpecId2,
          goodsName: this.data.goodsName1
        }
      ],
      deliveryCharge: 1
    }
    wx.$API['workService/orderadd'](request).then((response) => {
      this.setData({
        inTradeNo: response.data[0].inTradeNo
      })
      wx.navigateTo({
        url: '../../workService/paymentconfirm/index?inTradeNo=' + this.data.inTradeNo + '&goodsName=' + this.data.goodsName
      })
    }).catch(error => {
      wx.showToast({
        title: error,
        icon: "none"
      })
      console.log('getSwitch error', error);
    })
  },
  submitbtn () {
    if (this.data.showblock === true) {
      wx.showToast({
        title: '文件名称不能为空',
        icon: 'none',
        duration: 3000
      })
    } else if (this.data.goodsQuantity == '') {
      wx.showToast({
        title: '申请份数不能为空',
        icon: 'none',
        duration: 3000
      })
    } else {
      if (this.data.isObey == true) {
        this.getSwitch();
      }
      else {
        this.getdetailList();
      }
    }
  },
  eventdetail () {
    wx.navigateTo({
      url: '../../workService/Recordprocessing/index'
    })
  },
  address: function () {
    var ids = 1;
    wx.navigateTo({
      url: '../../workService/adaddress/index?ids=' + ids
    })
  },
  editaddress () {
    wx.redirectTo({
      url: '../../workService/selectadaddress/index'
    })
  },
  // 加减数量
  addshop: function () {
    var goodsQuantity = this.data.goodsQuantity;
    // 不作过多考虑自增1
    goodsQuantity++;
    // 只有大于一件的时候，才能normal状态，否则disable状态
    var minusStatus = goodsQuantity < 1 ? 'disabled' : 'normal';
    // 将数值与状态写回
    this.setData({
      goodsQuantity: goodsQuantity,
      minusStatus: minusStatus
    });
  },
  /* 点击减号 */
  removeshop: function () {
    var goodsQuantity = this.data.goodsQuantity;
    // 如果大于1时，才可以减
    if (goodsQuantity > 1) {
      goodsQuantity--;
    }
    // 只有大于一件的时候，才能normal状态，否则disable状态
    var minusStatus = goodsQuantity <= 1 ? 'disabled' : 'normal';
    // 将数值与状态写回
    this.setData({
      goodsQuantity: goodsQuantity,
      minusStatus: minusStatus
    });
  },
  bindkeyfile (e) {
    this.setData({
      goodsName: e.detail.value
    })
  },
  notekeyvalue (e) {
    this.setData({
      note: e.detail.value
    })
  }
})